<template>
   <div class="app-container">
      <a-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px" layout="inline">
         <a-form-item label="登录地址" field="ipaddr">
            <a-input
               v-model="queryParams.ipaddr"
               placeholder="请输入登录地址"
               clearable
               style="width: 240px;"
               @keyup.enter="handleQuery"
            />
         </a-form-item>
         <a-form-item label="用户名称" field="userName">
            <a-input
               v-model="queryParams.userName"
               placeholder="请输入用户名称"
               clearable
               style="width: 240px;"
               @keyup.enter="handleQuery"
            />
         </a-form-item>
         <a-form-item label="状态" field="status">
            <a-select
               v-model="queryParams.status"
               placeholder="登录状态"
               clearable
               style="width: 240px"
            >
               <a-option
                  v-for="dict in sys_common_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </a-select>
         </a-form-item>
         <a-form-item label="登录时间" style="width: 308px">
            <a-range-picker
               v-model="dateRange"
               value-format="YYYY-MM-DD"
               type="daterange"
               range-separator="-"
               start-placeholder="开始日期"
               end-placeholder="结束日期"
            ></a-range-picker>
         </a-form-item>
         <a-form-item>
            <a-button type="primary" class="button-margin-right"  @click="handleQuery">
              <template #icon>
                <icon-Search/>
              </template>
              搜索
            </a-button>
            <a-button @click="resetQuery">
              <template #icon>
                <icon-refresh />
              </template>
              重置
            </a-button>
         </a-form-item>
      </a-form>

      <a-row :gutter="10" class="mb8">
         <a-col :span="1.5">
            <a-button
               status="danger"
               type="primary"
               plain
               :disabled="multiple"
               @click="handleDelete"
               v-hasPermi="['system:logininfor:remove']"
            >
              <template #icon>
                <icon-delete/>
              </template>
              删除
            </a-button>
         </a-col>
         <a-col :span="1.5">
            <a-button
               status="danger"
               type="primary"
               plain
               @click="handleClean"
               v-hasPermi="['system:logininfor:remove']"
            >
              <template #icon>
                <icon-delete/>
              </template>
              清空
            </a-button>
         </a-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </a-row>

      <a-table ref="logininforRef" v-loading="loading" :data="logininforList"
               row-key="infoId" :row-selection="rowSelection"
               v-model:selectedKeys="selectedIds" :pagination="false" @selection-change="handleSelectionChange"
               :default-sort="defaultSort" @change="handleSortChange">
        <template #columns>
          <a-table-column title="访问编号" align="center" data-index="infoId" />
          <a-table-column title="用户名称" align="center" data-index="userName" :show-overflow-tooltip="true" sortable="custom" :sort-orders="['descending', 'ascending']" />
          <a-table-column title="地址" align="center" data-index="ipaddr" :show-overflow-tooltip="true" />
          <a-table-column title="登录地点" align="center" data-index="loginLocation" :show-overflow-tooltip="true" />
          <a-table-column title="操作系统" align="center" data-index="os" :show-overflow-tooltip="true" />
          <a-table-column title="浏览器" align="center" data-index="browser" :show-overflow-tooltip="true" />
          <a-table-column title="登录状态" align="center" data-index="status">
            <template #cell="{record}">
              <dict-tag :options="sys_common_status" :value="record.status" />
            </template>
          </a-table-column>
          <a-table-column title="描述" align="center" data-index="msg" />
          <a-table-column title="访问时间" align="center" data-index="loginTime" sortable="custom" :sort-orders="['descending', 'ascending']" width="180">
            <template #cell="{record}">
              <span>{{ parseTime(record.loginTime) }}</span>
            </template>
          </a-table-column>
        </template>
      </a-table>

     <!--分页-->
     <div style="margin-top: 30px;float: right">
       <a-pagination :total="total" show-total show-jumper show-page-size
                     v-model:current="queryParams.pageNum"
                     v-model:page-size="queryParams.pageSize"
                     @change="onPageChange"
                     @page-size-change="onPageSizeChange"
       />
     </div>
   </div>
</template>

<script setup name="Logininfor">
import { list, delLogininfor, cleanLogininfor } from "@/api/monitor/logininfor";
import {getCurrentInstance, ref} from "vue";

const { proxy } = getCurrentInstance();
const { sys_common_status } = proxy.useDict("sys_common_status");

const logininforList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const total = ref(0);
const dateRange = ref([]);
const defaultSort = ref({ prop: "loginTime", order: "descending" });

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  ipaddr: undefined,
  userName: undefined,
  status: undefined,
  orderByColumn: undefined,
  isAsc: undefined
});
/** 开启表格多选  */
const selectedIds = ref([]);
const rowSelection = reactive({
  type: 'checkbox',
  showCheckedAll: true,
  onlyCurrent: false,
});
// 修改按钮生效
const single = computed(() => {
  return selectedIds.value.length !== 1
});
// 删除按钮生效
const multiple = computed(() => {
  return !selectedIds.value.length
});

const onPageChange = (current) => {
  queryParams.value.pageNum = current
  getList()
};
const onPageSizeChange = (pageSize) => {
  queryParams.value.pageSize = pageSize
  getList()
};


/** 查询登录日志列表 */
function getList() {
  loading.value = true;
  list(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    logininforList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  proxy.$refs["logininforRef"].sort(defaultSort.value.prop, defaultSort.value.order);
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item);
  multiple.value = !selection.length;
}
/** 排序触发事件 */
function handleSortChange(column, prop, order) {
  queryParams.value.orderByColumn = column.prop;
  queryParams.value.isAsc = column.order;
  getList();
}
/** 删除按钮操作 */
function handleDelete(row) {
  const infoIds = row.infoId || ids.value;
  proxy.$confirm('是否确认删除访问编号为"' + infoIds + '"的数据项?').then(function () {
    return delLogininfor(infoIds);
  }).then(() => {
    getList();
    proxy.$message.success("删除成功");
  }).catch(() => {});
}
/** 清空按钮操作 */
function handleClean() {
  proxy.$confirm("是否确认清空所有登录日志数据项?").then(function () {
    return cleanLogininfor();
  }).then(() => {
    getList();
    proxy.$message.success("清空成功");
  }).catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download("monitor/logininfor/export", {
    ...queryParams.value,
  }, `config_${new Date().getTime()}.xlsx`);
}

getList();
</script>
